home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / WebsiteX5 / wsx5_free.exe / {app} / Res / nr_imemail.inc.php < prev    next >
PHP Script  |  2011-08-17  |  4KB  |  127 lines

  1. <?php
  2.   //Incomedia WebSite X5 EMail Class. All rights reserved.
  3.  
  4.     class imEMail {
  5.         var $from;
  6.         var $to;
  7.         var $subject;
  8.         var $charset;
  9.         var $text;
  10.         var $html;
  11.  
  12.         var $attachments;
  13.  
  14.         function imEMail($from,$to,$subject,$charset) {
  15.             $this->from = $from;
  16.             $this->to = $to;
  17.             $this->subject = $subject;
  18.             $this->charset = $charset;
  19.         }
  20.  
  21.         function setFrom($from) {
  22.             $this->from = $from;
  23.         }
  24.  
  25.         function setTo($to) {
  26.             $this->to = $to;
  27.         }
  28.  
  29.         function setSubject($subject) {
  30.             $this->subject = $subject;
  31.         }
  32.  
  33.         function setCharset($charset) {
  34.             $this->charset = $charset;
  35.         }
  36.  
  37.         function setText($text) {
  38.             $this->text = $text;
  39.         }
  40.  
  41.         function setHTML($html) {
  42.             $this->html = $html;
  43.         }
  44.  
  45.         function attachFile($name,$content,$mime_type) {
  46.             $attachment['name'] = $name;
  47.             $attachment['content'] = base64_encode($content);
  48.             $attachment['mime_type'] = $mime_type;
  49.             $this->attachments[] = $attachment;
  50.         }
  51.  
  52.         function send() {
  53.             $headers = "";
  54.             $msg = "";
  55.  
  56.             if($this->from == "" || $this->to == "" || ($this->text == "" && $this->html == ""))
  57.                 return false;
  58.  
  59.             $boundary_file = md5(time() . "_attachment");
  60.             $boundary_alt = md5(time() . "_alternative");
  61.  
  62.             $headers .= "From: " . $this->from . "\n";
  63.             $headers .= "Message-ID: <" . time() . rand(0,9) . rand(0,9) . "@websitex5.users>\n";
  64.             $headers .= "X-Mailer: WebSiteX5 Mailer\n";
  65.             $headers .= "MIME-Version: 1.0\n";
  66.  
  67.             if(is_array($this->attachments)) {
  68.                 $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary_file . "\"\n\n";
  69.                 $headers .= "--" . $boundary_file . "\n";
  70.             }
  71.  
  72.             if($this->html == "") {
  73.                 $headers .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . "\n";
  74.                 if (strtolower($this->charset) != "utf-8")
  75.                     $headers .= "Content-Transfer-Encoding: 7bit\n";
  76.                 $msg .= $this->text . "\n\n";
  77.             }
  78.             else if($this->text == "") {
  79.                 $headers .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . "\n";
  80.             if (strtolower($this->charset) != "utf-8")
  81.                     $headers .= "Content-Transfer-Encoding: 7bit\n";
  82.                 $msg .= $this->html . "\n\n";
  83.             }
  84.             else {
  85.                 $headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary_alt . "\"\n";
  86.  
  87.                 $msg .= "--" .$boundary_alt . "\n";
  88.                 $msg .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . "\n";
  89.                 if (strtolower($this->charset) != "utf-8")
  90.                 $msg .= "Content-Transfer-Encoding: 7bit\n";
  91.                 $msg .= "\n";
  92.                 $msg .= $this->text . "\n\n";
  93.  
  94.                 $msg .= "--" . $boundary_alt . "\n";
  95.               $msg .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . "\n";
  96.               if (strtolower($this->charset) != "utf-8")
  97.                     $msg .= "Content-Transfer-Encoding: 7bit\n";
  98.                 $msg .= "\n";
  99.                 $msg .= $this->html . "\n\n";
  100.  
  101.                 $msg .= "--" . $boundary_alt . "--\n\n";
  102.             }
  103.  
  104.             if(is_array($this->attachments)) {
  105.                 foreach($this->attachments as $attachment) {
  106.                     $msg .= "--" . $boundary_file . "\n";
  107.                     $msg .= "Content-Type: " . $attachment["mime_type"] . "; name=\"" . $attachment["name"] . "\"\n";
  108.                     $msg .= "Content-Transfer-Encoding: base64\n";
  109.                     $msg .= "Content-Disposition: attachment; filename=\"" . $attachment["name"] . "\"\n\n";
  110.                     $msg .= chunk_split($attachment["content"]) . "\n\n";
  111.                 }
  112.  
  113.                 $msg .= "--" . $boundary_file . "--\n\n";
  114.             }
  115.  
  116.             ini_set("sendmail_from", $this->from);
  117.  
  118.             $r = @mail($this->to, $this->subject, $msg, $headers, "-f" . $this->from);
  119.             if(!$r) {
  120.                 $headers = "To: " . $this->to . "\n" . $headers;
  121.                 $r = @mail($this->to, $this->subject, $msg, $headers);
  122.             }
  123.             return $r;
  124.         }
  125.     }
  126.  
  127. // End of file imemail.inc.php